//
//  SCNConstants.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/7.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit
import Alamofire

/**
 *   打印日志
 **/
public func printLog<T>(_ message: T,
                   file: String = #file,
                   method: String = #function,
                   line: Int = #line)
{
    print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")
}



class SCNConstants: NSObject {
    /**
     *   请求完成
     **/
    static let RequestComplete = "RequestComplete"
    /**
     *   请求失败
     **/
    static let RequestFail = "RequestFail"
    /**
     *   当前设备屏幕的宽带
     **/
    static let SCREENWIDTH = UIScreen.main.bounds.width
    
    /**
     *   当前设备屏幕的高度
     **/
    static let SCREENHEIGHT = UIScreen.main.bounds.height
    /**
     *   导航栏高度
     **/
    static let NavigationHeight: CGFloat = UIDevice.current.isiPhoneX() ? 88.0 : 64.0
    
    
    /**
     *   提示消息
     **/
    static let SCN_NO_NET_MSG = "网络好像不给力"
    
    /**
     *  颜色
     */
    static func colorWith255RGBA(_ r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor{
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a);
    }
    
    /**
     *  16进制转RGB
     */
    static func colorFromHexValue(_ hex:UInt,alpha:CGFloat) -> UIColor{
        
        return UIColor(
            red: CGFloat((hex & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((hex & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(hex & 0x0000FF) / 255.0,
            alpha: CGFloat(alpha)
        )
        
    }
    /**
     *  字符串进制转RGB
     */
    static func colorWithHexString (_ hex:String) -> UIColor {
   
      
        var cString: String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
            
        
            if (cString.hasPrefix("#"))
            {
                cString = cString.substring(from: cString.index(after: cString.startIndex))
    
            }
            
            
            
            if cString.characters.count != 6 {
                
                return .black
                
            }
        
      
        let rRange = cString.startIndex ..< cString.index(cString.startIndex, offsetBy: 2)
        let rStr = cString.substring(with: rRange)
        
        let gRange = cString.index(cString.startIndex, offsetBy: 2) ..< cString.index(cString.startIndex, offsetBy: 4)
        let gStr = cString.substring(with: gRange)
        
        let bIndex = cString.index(cString.endIndex, offsetBy: -2)
        let bStr = cString.substring(from: bIndex)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rStr).scanHexInt32(&r)
        Scanner(string: gStr).scanHexInt32(&g)
        Scanner(string: bStr).scanHexInt32(&b)
      
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
        
            
    }
    
    
    // 2588e5
    static let rootBackgroundColor = SCNConstants.colorWithHexString("18aaf3")
    
    static let tableViewBackgroundColor = colorWith255RGBA(247, g: 247, b: 251, a: 1)
    
    /**
     *  网络是否可用
     */
    static func networkIsReachable()->Bool{
        
        var ret:Bool!
        let manager = NetworkReachabilityManager()
        
        if  manager?.isReachable ?? false {
            
            //            if ((manager?.isReachableOnEthernetOrWiFi) != nil) {
            //                //do some stuff
            //            }else if(manager?.isReachableOnWWAN)! {
            //                //do some stuff
            //            }
            //
            ret = true
        }
        else {
            ret = false
        }
        return ret
        
    }
    
    /**
     *  手机号码验证
     *
     *  @param mobileNum 手机号码
     *
     */
    static func isMobileNumber(_ number:String)->Bool{
        
        let regextestmobile:NSPredicate = NSPredicate(format: "SELF MATCHES %@", "^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$")
        
        if regextestmobile.evaluate(with: number) == true {
            
            return true
        }else{
            
            return false
        }
        
    }
    
    /**
     *  对图片尺寸进行压缩
     *
     *  @param image   UIImage
     *  @param newSize CGSize
     *
     *  @return UIImage
     */
    static func imageScaledToSize(_ image:UIImage,newSize:CGSize)->UIImage{
        
        UIGraphicsBeginImageContext(newSize)
        image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
        
    }

    
}

extension UIDevice {
    public func isiPhoneX() -> Bool {
        return UIScreen.main.bounds.height == 812 && UIScreen.main.scale == 3
    }
    public func isiPhone5() -> Bool {
        return UIScreen.main.bounds.height == 568 && UIScreen.main.scale == 2
    }
    public func isiPhone6() -> Bool {
        return UIScreen.main.bounds.width == 375 && UIScreen.main.scale == 2
    }
    public func isiPhone6p() -> Bool {
        return UIScreen.main.bounds.width == 414 && UIScreen.main.scale == 3
    }
}
